<?
//	if($errorMsg) echo "<br><div class=errormsg>$errorMsg</div><br>";
//	$errorMsg = "";
        if($errorMsg) {
 	      include ('errorMsg.html');       	                
        }
        if($okMsg) {
          include ('okMsg.html');
        } else {
        }
//---------------------------------------------------------------------------
function get_stats1($id,$domain){
        global $session_db_name, $dbh,$sid,$baseURL,$webDIR;
	if($domain) $and = " and domain='$domain' ";
		$SQL = "select date,count(ip) a ,count(distinct ip) as u  from reseller_referers where id='$id' $and group by date order by date desc;";

	echo "<!--$SQL-->\n\n";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	
        for($i=0;$i<=40;$i++){
        	$key = date ("Y-m-d", mktime (0,0,0,date("m"),date("d")-$i,date("Y")));
        	$data[$key] = "<td>-</td><td>-</td>";
        }
        while($row=mysql_fetch_array($result)){
        	 $data[$row[date]] = "<td>".$row[u]."</td><td>".($row[a]-$row[u])."</td>";
        }
	reset($data);
	$list = "<tr><th>" . translate('date') . "</th><th>" . translate('uniq') . "</th><th>" . translate('reloads') . "</th></tr>";
	while(list($k,$v) = each($data)){
	        $params[sid] = $sid;
	        $params[action] = "stats";
		$params[subact] = "referer";
		$params[stats_date] = "$k";
		$params[stats_domain] = "$domain";
	        $url = "$baseURL/?".encode_params($params);
		$list .= "<tr align='center'><td><a href='$url'>$k</a></td>$v</tr>";
	}
        return $list;
}
//---------------------------------------------------------------------------
function get_stats2($id,$domain,$date){
        global $session_db_name, $dbh,$sid,$baseURL,$webDIR;
	 if($domain) $and = " and domain='$domain' ";
	 if($date) $and .= " and date='$date' ";
		$SQL = "select referer,count(ip) as a,count(distinct ip) as u  from reseller_referers where id='$id' $and group by referer order by u desc;";
	echo "<!--$SQL-->\n\n";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	if($date) $list = "<tr><th colspan='3'>Stats for $date </th></tr>";
	$list .= "<tr><th>referrer</th><th>unique</th><th>reloads</th></tr>";
        while($row=mysql_fetch_array($result)){
		$title = $row[referer];
		if(!$row[referer]) $row[referer] = "none";
		$params[sid] = $sid;
		$params[action] = "redirect";
		$params[url] = $row[referer];
		$url = "$baseURL/?".encode_params($params);
		if(strlen($row[referer]) > 60) $row[referer] = substr($row[referer],0,60)."...";
		$list .= "<tr><td><a title='$title' target='_blank' href='$url'>$row[referer]</a></td><td align='center'>".$row[u]."</td><td align='center'>".($row[a]-$row[u])."</td></tr>";
        }
        return $list;
}
//---------------------------------------------------------------------------
function get_domains_select($id,$domain){
        global $session_db_name, $dbh;
                $SQL = "select domain from domains where id='$id'";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	$list = "<option value=''>" . translate('all_domains') . "</option>";
	$list .= "<option value='duoservers.com'>duoservers.com</option>";
        while($row=mysql_fetch_array($result)){
		$list .= "<option value='$row[domain]'>$row[domain]</option>";
        }
	$list = str_replace("'$domain'","$domain selected",$list);
        return $list;
}
//---------------------------------------------------------------------------

        $params[sid] = $sid;
        $params[action] = "stats";
        $url = "$baseURL/?".encode_params($params);

?> 
<table width="450">
  <form action='<?=$url;?>' method='POST'>
    <tr> 
      <td><font class="text"><b><?=translate('traffic_stats');?></b></font></td>
    </tr>
    <tr> 
      <td> 
        <br>
        <?=translate('traffic_stats_desc');?><br>
          <br>
        <?
        $params[sid] = $sid;
        $params[action] = "stats";
        $params[subact] = "period";
        $url = "$baseURL/?".encode_params($params);
        $params="";
        ?><?=translate('traffic_stats_desc2', array("url"=>$url));?>

<br>
          <br>
        <?
        $params[sid] = $sid;
        $params[action] = "stats";
        $params[subact] = "referer";
        $url = "$baseURL/?".encode_params($params);
        $params="";
?>
        <?=translate('traffic_stats_desc3', array('url' => $url));?>
	<br>
          <br>
	 <div align="center">
          <select name='stats_domain' onChange='submit();'>
            <? echo get_domains_select($id,$stats_domain);?>
          </select>
          <input type='hidden' name='subact' value='<?=$subact;?>'><br>
	</div>
      </td>
    </tr>
  </form>
</table>
<br>
<br>
<? 
if(!$subact) $subact='period';
if($subact=='period') $wd = "450";
else $wd = "540";
?>
<TABLE border=1 class='bTable' cellspacing='0' width="<?=$wd;?>">
<? 
if($subact=='period')	echo get_stats1($id,$stats_domain);
if($subact=='referer')  echo get_stats2($id,$stats_domain,$stats_date);
?>
</TABLE>

